import java.awt.Point;
import java.util.HashMap;


public class HTMLIZER {

	private Node root;
	private HashMap<Integer, Integer> spaceLevel = new HashMap<Integer, Integer>();
	public int circleDiameter = 30; 
	public int height = 1000;
	public int width = 1000;
	
	
	public HTMLIZER(Node rootNode) {
		root = rootNode;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Node rootNode = new Node("root");
		

		Node childNode1 = new Node("child1");
		Node childNode2 = new Node("child2");
		Node childNode3 = new Node("child3");
		Node childchildNode = new Node("child4");

		rootNode.addChild(childNode1);
		rootNode.addChild(childNode2);
		rootNode.addChild(childNode3);
		childNode1.addChild(childchildNode);
		
		HTMLIZER html = new HTMLIZER(rootNode);
		
		html.getHTML(0,html.getRoot(),new Point(0,0));
	}

	private Node getRoot() {
		
		return root;
	}

	private void getHTML(int deep,Node n,Point orig) {
		if( deep == 0 )System.out.println(getHead()+"<body><svg width=\""+width+"\" height=\""+height+"\" xmlns=\"http://www.w3.org/2000/svg\">");
		
		if (spaceLevel.get(deep) == null )
			spaceLevel.put(deep, root.getChildren().size());
		else
			spaceLevel.put(deep, root.getChildren().size() + spaceLevel.get(deep));
	
		int y =circleDiameter + deep * circleDiameter*2;
		int x =  (spaceLevel.get(deep) * circleDiameter + circleDiameter);
		
		if (deep != 0)
			System.out.println("<line x1=\""+orig.x+"\" y1=\""+orig.y+"\" x2=\""+x+"\" y2=\""+y+"\" style=\"stroke:rgb(99,99,99);stroke-width:2\"/>");

		System.out.println("<circle class=\"Node\" id=\""+n.getName()+"\" cx=\""+x+"\" cy=\""+y+"\" r=\""+circleDiameter/2+"\" />");
		
		
		if(n.getChildren().size() > 0)
			for (Node nd : n.getChildren())
				getHTML(deep+1, nd,new Point(x,y));
		
		
		if( deep == 0 )System.out.println("</svg></body></html>");
	}
	
	public String getHead(){
		return "<html  xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"fr\" lang=\"fr\"><head>"+
		"<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.5.2.min.js\"></script>"+
		"<script type=\"text/javascript\">"+
			"$(document).ready("+
				"function(){"+
					"$('.Node').click("+
						"function(){"+
							"alert('mon nom : '+$(this).attr('id'));"+
			"});});"+
		"</script></head>";
	}

}
